<?php
/**
 * @TODO: Documenting this service
 *
 * Google Analytics admin library
 * @author Hasnain Khan <hasnain.khan@newgenbd.net>
 *
 * Services
 */

class service_l_analytics {
    const default_tracker_id = 1;

    public static function ga_connect(){
        define('ga_email','ecopoxy@gmail.com');
        define('ga_password','ecopoxysystemscanada');
        define('ga_profile_id','41937276');

        return new gapi_services_l_analytics(ga_email, ga_password);
    }

    public function ga_request($ga, $start, $end, $is_day){
                $ga->requestReportData(
                                ga_profile_id,
                                $is_day ? array('day','month','year') : array('month','year'),
//                                array('day','month','year','browser'),
                                array('newVisits','uniquepageviews','pageviews','visits','bounces','entrances'),
                                array('-year','-month') ,
//                                array('browser','browserVersion'),
                                $filter=null,
                                $start_date=$start,
                                $end_date=$end,
                                $start_index=1,
                                $max_results=1000
                                );

                  return $ga;
    }

    public static function get_calendar(){

        $calendar = new stdClass();

        $calendar->numeric_month = array("01","02","03","04","05","06","07","08","09","10","11","12");
        $calendar->calendar_month = array("January","February","March","April","May","June","July","August","September","October","November","December");

        return $calendar;
    }

    public function set_date($start, $end){

        $date_range = new stdClass();
        $date_range->ga = new stdClass();
        $date_range->jquery = new stdClass();

        if($start)
        {
            $date_range->jquery->start=$start;
            $month = strtok($start, "/");
            $date = strtok("/");
            $year = strtok("/");

            $date_range->ga->start = $year."-".$month."-".$date;
        }
        else
        {
            $date_range->ga->start = '2011-02-01';
            $date_range->jquery->start="02/01/2011";
        }

        if($end)
        {
            $date_range->jquery->end=$end;
            $month = strtok($end, "/");
            $date = strtok("/");
            $year = strtok("/");

            $date_range->ga->end = $year."-".$month."-".$date;
        }
        else
        {
            $date_range->ga->end = date("Y-m-d");
            $date_range->jquery->end = date("m/d/Y");
        }

        return $date_range;
    }

    public function get_data($getResults, $is_day){
        $i=0;
        foreach($getResults as $result)
        {
            $array[$i][0] = 1000*mktime(0, 0, 0, (int)$result->getMonth(), $is_day ? (int)$result->getDay() : 1, (int)$result->getYear());
            $array[$i][1] = (int)$result->getVisits();
            $array[$i][2] = (int)$result->getPageviews();
            $i++;
        }

        return $array;
    }

    public function split_array($array)
    {
        $i=0;
        for(;$i<count($array);$i++)
        {
            $array1[$i][0] = $array[$i][0];
            $array1[$i][1] = $array[$i][1];
            $array2[$i][0] = $array[$i][0];
            $array2[$i][1] = $array[$i][2];
        }

        return array($array1, $array2);
    }


    public function get_object($user_id_object) {
       return get_object($user_id_object, "setting_m_analytics");
    }

    public function get_trackers(){
        $obj = new setting_m_analytics();
        return $obj->get();
    }
    
    public function get_tracker($tracker_id) {
        return $this->get_object($tracker_id);
    }

    public static function get_default_tracker() {
        $default_tracker = new service_l_analytics();
        return $default_tracker->get_tracker(self::default_tracker_id);
    }

    public static function is_tracking_enabled()
    {
        return service_l_core::get_global_setting('ga_tracking_enabled');
    }

    // edits the tracker information if user has permission to
    public function save_tracker(array $array_data) {

        $t = new setting_m_analytics();

        if($array_data['tracker_id'])
            $t->get_by_id($array_data['tracker_id']);

        $t->tracking_id = $array_data['ga_tracking_id'];
        $t->site_id = $array_data['site_id'];
        $t->enable_page_tracking = $array_data['page_tracking'];
        $t->ecommerce_tracking   = $array_data['ecommerce_tracking'];
        $t->event_tracking       = $array_data['event_tracking'];

        return $t->save();
    }

    // first check if this user has permission to alder this this tracker then
    // delete this tracker if this tracker exists
    public function delete_tracker($tracker_id) {

        $t = new setting_m_analytics();
        $t = $t->get_by_id($tracker_id);

        return $t->delete();
    }

    public function print_date(array $date){

        echo "year | month | date | <br>";
        for($i=0; $i<count($date);$i++)
        {
            for($j=0; $j<count($date[$i]);$j++)
            {
                echo $date[$i][$j]." | ";
            }
            echo "<br>";
        }
    }

    public function sort_by_year(array $date)
    {
        $i=0;
        $j=0;
        $temp = $date[0][0]; //the second index is the actual field, 0 is year, 1 is month
        $ga_ordered_date= array();    // instead of sorting the saem array i used this array to be copied in a systematic order

        //first sort by year
        foreach($date as $year)
        {
            if($temp == $year[0])
                $ga_ordered_date[$i][$j++]=$year;
            else
            {
                $ga_ordered_date[$i] = $this->sort_by_month($ga_ordered_date[$i]);
                $temp = $year[0];
                $i++;
                $j=0;
                $ga_ordered_date[$i][$j++]=$year;
            }
        }
        $ga_ordered_date[$i] = $this->sort_by_month($ga_ordered_date[$i]);

        $ga_ordered_date = array_reverse($ga_ordered_date, false);


        return $ga_ordered_date;
    }

    public function sort_by_month(array $date)
    {
        $temp = $date[0][1];    //the second index is the actual field, 0 is year, 1 is month
        $i=0;
        $j=0;

        $temp_month= array();

        //first sort my year
        foreach($date as $month)
        {
            if($temp == $month[1])
                $temp_month[$i][]=$month;
            else
            {
                $temp = $month[1];
                $i++;
                $j=0;
                $temp_month[$i][$j++]=$month;
            }
        }

        $temp_month = array_reverse($temp_month);

        return $temp_month;
    }

    public function get_region_data($getResults)
    {
        $i=0;
        $region_data = array();

        foreach($getResults as $result)
        {
          $region_data[$i][0] = $result->getCountry();
          $region_data[$i][1] = $result->getVisits();
          $i++;
        }

        return $region_data;
    }

    public function convert_date_query_to_sql($date_)
    {
        $month = strtok($date_, "/");
        $date = strtok("/");
        $year = strtok("/");

        return $year."-".$month."-".$date;;
    }

    public static function sort_array($arr)
    {
            function cmp($a, $b)
            {
                 if ($a == $b) {
                        return 0;
                    }
                    return ($a < $b) ? -1 : 1;
            }

        usort($arr, 'cmp');

        return $arr;
    }

    public function convert_date_sql_to_query($date_)
    {
    }
}

?>